package com.innerchic.weqiuqiu.assembly;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.innerchic.mapper.app.SuperAdapter;
import com.innerchic.mapper.log.Logger;
import com.innerchic.mapper.tool.SystemTool;
import com.innerchic.sponsor.Sponsor;
import com.innerchic.sponsor.other.Subject;
import com.innerchic.weqiuqiu.R;
import com.innerchic.weqiuqiu.adapter.MessageListAdapter;
import com.innerchic.weqiuqiu.app.RecyclerItemTouchHelper;
import com.innerchic.weqiuqiu.bean.MessageBean;
import com.innerchic.weqiuqiu.bean.MessageRemove;

/**
 * ================================================
 * 作    者：贾恒飞 >>> 17515250730@163.com
 * 项    目：wedudu
 * 日    期：2022/1/14 17:49
 * 包    名：com.innerchic.weqiuqiu.assembly
 * 描    述：消息球
 * Create by zFox from AndroidStudio4.2
 * ================================================
 */
public class MessageListView implements SuperAdapter.OnItemMonitorListener<MessageBean> {

    /**
     * 当前持有布局
     */
    private View messageListView;

    /**
     * 创建方法
     *
     * @param context 上下文
     */
    public void create(Context context) {
        //注册监听
        Sponsor.getDefault().register(this);
        //获取窗口管理器
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        //初始化消息列表
        WindowManager.LayoutParams layoutParams = initViewLayoutParams(context);
        initView(context);
        windowManager.addView(messageListView, layoutParams);
    }

    private MessageListAdapter messageListAdapter;

    /**
     * 初始化View
     *
     * @param context 上下文
     */
    private void initView(Context context) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        messageListView = layoutInflater.inflate(R.layout.view_message_list_layout, null);
        RecyclerView recyclerView = messageListView.findViewById(R.id.recyclerLy);
        messageListAdapter = new MessageListAdapter();
        RecyclerView.LayoutManager layoutManager = messageListAdapter.getLayoutManager();
        recyclerView.setLayoutManager(layoutManager);
        //删除默认动画时长
        RecyclerView.ItemAnimator itemAnimator = recyclerView.getItemAnimator();
        if (itemAnimator != null) {
            itemAnimator.setRemoveDuration(0);
        }
        messageListAdapter.setItemMonitorListener(this);
        messageListAdapter.bindRecyclerView(recyclerView, context);

        ItemTouchHelper.Callback callback = new RecyclerItemTouchHelper(messageListAdapter);
        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(callback);
        itemTouchHelper.attachToRecyclerView(recyclerView);
    }

    @Subject(thread = Subject.THREAD_MAIN)
    public void render(MessageBean messageBean) {
        messageListAdapter.appendAndRefreshData(messageBean);
    }

    @Subject(thread = Subject.THREAD_MAIN)
    public void remove(MessageRemove messageRemove) {
        String md5Key = messageRemove.getMd5Key();
        messageListAdapter.removeIf(data -> data.getMd5Key().equals(md5Key));
    }

    /**
     * 销毁
     */
    public void destroy() {
        Sponsor.getDefault().unregister(this);
    }

    /**
     * 创建布局参数
     *
     * @param context 上下文
     * @return WindowManager.LayoutParams
     */
    private WindowManager.LayoutParams initViewLayoutParams(Context context) {
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        }
        //悬浮窗弹出的位置
        layoutParams.gravity = Gravity.END | Gravity.BOTTOM;
        //防止悬浮窗出现之后点击其他view没有反应
        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        layoutParams.format = PixelFormat.RGBA_8888;
        //设置高度
        layoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        layoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        boolean navigationBar = SystemTool.checkDeviceHasNavigationBar(context);
        int navigationBarHeight = 30;
        if (navigationBar) {
            navigationBarHeight += SystemTool.getNavigationBarHeight(context);
        }
        layoutParams.x = 10;
        layoutParams.y = navigationBarHeight;
        Logger.d("底部高度:" + (navigationBarHeight - 30));
        return layoutParams;
    }

    @Override
    public void onMonitor(View monitorView, MessageBean data, int position, int type) {
    }

}
